package com.chenjun.core.service.sys.impl;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.chenjun.common.bean.Pagination;
import com.chenjun.common.model.DataGridModel;
import com.chenjun.core.dao.generator.entity.SysRole;
import com.chenjun.core.dao.generator.mapper.SysRoleMapper;
import com.chenjun.core.dao.module.SysRoleRelationMapper;
import com.chenjun.core.dao.module.SysRoleResourceRelationMapper;
import com.chenjun.core.service.activiti.SynRoleToActivitiService;
import com.chenjun.core.service.sys.SysRoleService;
@SuppressWarnings("serial")
@Service
@Transactional
public class SysRoleServiceImpl implements SysRoleService{

	private static final String ROLE_MAPPER = "RoleMapper";
    private static final Logger logger = Logger.getLogger(SysRoleServiceImpl.class);

    @Autowired
    private SysRoleMapper 			sysRoleMapper;
    @Autowired
    private SysRoleRelationMapper	sysRoleRelationMapper;
    @Autowired
    private SysRoleResourceRelationMapper	sysRoleResourceRelationMapper;
    
    @Autowired
	private SynRoleToActivitiService synRoleService;

	public SysRole get(String id){
		return sysRoleMapper.selectByPrimaryKey(id);
	}

	public int create(SysRole record){
		return sysRoleMapper.insert(record);
	}

	public int delete(String id){
		/*Map<String,String> params = new HashMap<String,String>(1);
		params.put("roleId", id);
		sysRoleRelationMapper.deleteByCondition(params);*/
		// 同步到activiti中
		synRoleService.deleteGroup(id);
		return sysRoleMapper.deleteByPrimaryKey(id);
	}

	public int update(SysRole record){
		return sysRoleMapper.updateByPrimaryKey(record);
	}

	public int createOrUpdate(SysRole record){
		record.setDisabled("0"); //默认启用
		int rows = 0;
		if (StringUtils.isBlank(record.getId())) {
			String roleID = UUID.randomUUID().toString();
			record.setId(roleID);
			Date date = Calendar.getInstance().getTime();
			record.setCreatedTime(date);
			rows = sysRoleMapper.insert(record);
			// 同步到activiti
			synRoleService.addGroup(record);
		} else {
			rows = sysRoleMapper.updateByPrimaryKeySelective(record);
		}
		return rows;
	}

	public int doCountByCondition(DataGridModel param){
		return sysRoleRelationMapper.countByCondition(param);
	}

	public List<SysRole> doListByCondition(DataGridModel param){
		return sysRoleRelationMapper.selectByCondition(param);
	}

	public Pagination doPagination(DataGridModel param){
		Pagination vo = new Pagination();
		List<SysRole> list = sysRoleRelationMapper.selectByCondition(param);
		param.setStart(0);
		param.setRows(0);
		int size = sysRoleRelationMapper.countByCondition(param);
		vo.setAaData(list);
		vo.setiTotalRecords(size);
		vo.setsEcho(param.getPage());
		vo.setiTotalDisplayRecords(size);
		return vo;
	}
}
